package models

import (
	"gorm.io/gorm"
	"order_srv/basic/config"
	"time"
)

// 优惠券表
type Coupon struct {
	Id         uint64         `gorm:"column:id;type:bigint UNSIGNED;primaryKey;not null;" json:"id"`
	CouponType uint8          `gorm:"column:coupon_type;type:tinyint UNSIGNED;comment:优惠卷类型 1-打折券 2-现金券;not null;" json:"coupon_type"` // 优惠卷类型 1-打折券 2-现金券
	Price      float64        `gorm:"column:price;type:decimal(10, 2);comment:优惠券金额;not null;" json:"price"`                                 // 优惠券金额
	UserId     uint64         `gorm:"column:user_id;type:bigint UNSIGNED;comment:用户id;not null;" json:"user_id"`                                // 用户id
	OrderId    uint64         `gorm:"column:order_id;type:bigint UNSIGNED;comment:订单id;not null;" json:"order_id"`                              // 订单id
	StartTime  time.Time      `gorm:"column:start_time;type:datetime(3);comment:开始时间;not null;" json:"start_time"`                            // 开始时间
	EndTime    time.Time      `gorm:"column:end_time;type:datetime(3);comment:结束时间;not null;" json:"end_time"`                                // 结束时间
	OperatorId uint64         `gorm:"column:operator_id;type:bigint UNSIGNED;comment:操作人id;not null;" json:"operator_id"`                      // 操作人id
	CreatedAt  time.Time      `gorm:"column:created_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"created_at"`
	UpdatedAt  time.Time      `gorm:"column:updated_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"updated_at"`
	DeletedAt  gorm.DeletedAt `gorm:"column:deleted_at;type:datetime(3);default:NULL;" json:"deleted_at"`
}

func (c *Coupon) TableName() string {
	return "coupon"
}

// 创建优惠券
func (c *Coupon) CreateCoupon() error {
	return config.DB.Create(c).Error
}

func (c *Coupon) CouponDetail(id uint64) error {
	return config.DB.Where("id = ?", id).First(c).Error
}
